//
//  ShopTimeViewController.m
//  Findif
//
//  Created by Mac on 2018/11/19.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "ShopTimeViewController.h"
#import "PGDatePickManager.h"

@interface ShopTimeViewController ()<PGDatePickerDelegate,UIPickerViewDelegate,UIPickerViewDataSource>{
    
    NSInteger _weekIndex;
}

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, strong)UILabel * dateLabel;
@property (nonatomic, strong)UILabel * timeLabel;
@property (nonatomic, strong)UIImageView * dateStartImageView;
@property (nonatomic, strong)UIImageView * dateEndImageView;
@property (nonatomic, strong)UIImageView * timeStartImageView;
@property (nonatomic, strong)UIImageView * timeEndImageView;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, strong)UIButton * dateStartButton;
@property (nonatomic, strong)UIButton * dateEndButton;
@property (nonatomic, strong)UIButton * timeStartButton;
@property (nonatomic, strong)UIButton * timeEndButton;
@property (nonatomic, weak)UIButton * saveButton;

@property (nonatomic, copy)NSString * type;

@property (nonatomic, strong)NSArray * dataArray;
@property (nonatomic, strong)UIView * bgView;
@property (nonatomic, strong)UIPickerView * pickerView;

@end

@implementation ShopTimeViewController

//block的实现方法
- (void)returnText:(ReturnSubBlock)block {
    
    self.returnSubBlock = block;
} 

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ColorWithRGB(0xEFF0F1);
    
    _weekIndex = 0;
    self.dataArray = @[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];
    [self setupNav];
    [self setupUI];
    [self setupPivkerView];
}

- (void)setupNav{
    
    self.title = @"营业时间";
    
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back(1)"] style:UIBarButtonItemStylePlain target:nil action:nil];
//    
//    self.navigationItem.backBarButtonItem = backItem;
}


- (void)setupUI{
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.userInteractionEnabled = YES;
    backView.layer.cornerRadius = 5;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;
    
    [self.view addSubview:self.backView];
    
    
    self.dateLabel = [self setupLabelWithTitle:@"工作日期"];
    [self.backView addSubview:self.dateLabel];
    self.timeLabel = [self setupLabelWithTitle:@"工作时间"];
    [self.backView addSubview:self.timeLabel];
    
    self.oneLineView = [self setupLineView];
    [self.backView addSubview:self.oneLineView];
    self.twoLineView = [self setupLineView];
    [self.backView addSubview:self.twoLineView];
    self.threeLineView = [self setupLineView];
    [self.backView addSubview:self.threeLineView];
    
    self.dateStartImageView = [self setupImageView];
    [self.backView addSubview:self.dateStartImageView];
    self.dateEndImageView = [self setupImageView];
    [self.backView addSubview:self.dateEndImageView];
    self.timeStartImageView = [self setupImageView];
    [self.backView addSubview:self.timeStartImageView];
    self.timeEndImageView = [self setupImageView];
    [self.backView addSubview:self.timeEndImageView];
    
    self.dateStartButton = [self setupDateButtonWithTitle:@"每周开始日期"];
    [self.backView addSubview:self.dateStartButton];
    self.dateEndButton = [self setupDateButtonWithTitle:@"每周结束日期"];
    [self.backView addSubview:self.dateEndButton];
    self.timeStartButton = [self setupDateButtonWithTitle:@"每日开始时间"];
    [self.backView addSubview:self.timeStartButton];
    self.timeEndButton = [self setupDateButtonWithTitle:@"每日结束时间"];
    [self.backView addSubview:self.timeEndButton];
    
    UIButton * saveButton = [UIButton new];
    
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [saveButton setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(clickSaveButton) forControlEvents:UIControlEventTouchUpInside];
    saveButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    [saveButton setBackgroundColor:ColorWithRGB(0xd3d3d3)];
    saveButton.enabled = NO;
    saveButton.layer.cornerRadius = 5;
    saveButton.layer.masksToBounds = YES;
    
    self.saveButton = saveButton;
    
    [self.view addSubview:self.saveButton];
}

- (void)viewDidLayoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.height.mas_equalTo(100 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(10 * SCREENH6 + kNavHeight + kStatusBarHeight);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backView);
        make.height.mas_equalTo(1 * SCREENH6);
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.backView).offset(15 * SCREENH6);
    }];
    
    [self.dateStartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(95 * SCREENH6);
        make.centerY.mas_equalTo(self.dateLabel);
        make.height.mas_equalTo(45 * SCREENH6);
        make.width.mas_equalTo(85 * SCREENH6);
    }];
    
    [self.dateStartImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dateStartButton.mas_trailing).offset(3 * SCREENH6);
        make.centerY.mas_equalTo(self.dateStartButton);
        make.width.mas_equalTo(9 * SCREENH6);
        make.height.mas_equalTo(6 * SCREENH6);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1 * SCREENH6);
        make.leading.mas_equalTo(self.dateStartImageView.mas_trailing).offset(15 * SCREENH6);
        make.centerY.mas_equalTo(self.dateStartImageView);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.dateEndButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.twoLineView.mas_trailing).offset(15 * SCREENH6);
        make.centerY.height.width.mas_equalTo(self.dateStartButton);
    }];
    
    [self.dateEndImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dateEndButton.mas_trailing).offset(3 * SCREENH6);
        make.centerY.width.height.mas_equalTo(self.dateStartImageView);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.timeStartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.width.mas_equalTo(self.dateStartButton);
        make.centerY.mas_equalTo(self.timeLabel);
    }];
    
    [self.timeStartImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.width.mas_equalTo(self.dateStartImageView);
        make.centerY.mas_equalTo(self.timeStartButton);
    }];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.leading.mas_equalTo(self.twoLineView);
        make.centerY.mas_equalTo(self.timeStartImageView);
    }];
    
    [self.timeEndButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.threeLineView.mas_trailing).offset(15 * SCREENH6);
        make.centerY.height.width.mas_equalTo(self.timeStartButton);
    }];
    
    [self.timeEndImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.timeEndButton.mas_trailing).offset(3 * SCREENH6);
        make.centerY.width.height.mas_equalTo(self.timeStartImageView);
    }];
    
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.backView.mas_bottom).offset(200 * SCREENH6);
        make.height.mas_equalTo(35 * SCREENH6);
        make.width.mas_equalTo(250 * SCREENH6);
    }];
    
}


- (void)presentDateController{
    
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    datePickManager.isShadeBackgroud = true;
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    datePicker.datePickerType = PGDatePickerType1;
    datePicker.isHiddenMiddleText = false;
    datePicker.datePickerMode = PGDatePickerModeTimeAndSecond;
    //    datePicker.maximumDate = [NSDate date];
    datePicker.language = @"zh-Hans";
    [self presentViewController:datePickManager animated:false completion:nil];
}


#pragma PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    
    NSString * date = [NSString stringWithFormat:@"%li:%02li:%02li",(long)dateComponents.hour,dateComponents.minute,dateComponents.second];
    
    
    if ([self.type isEqualToString:@"timeStart"]) {
        
        self.timeStartButton.selected = YES;
        [self.timeStartButton setTitle:date forState:UIControlStateNormal];
    }else{
        
        self.timeEndButton.selected = YES;
        [self.timeEndButton setTitle:date forState:UIControlStateNormal];
    }
    [self changButtonState];
}


- (void)setupPivkerView{
    
    self.bgView = [[UIView alloc]init];
    self.bgView.frame = CGRectMake(0, kScreenH, kScreenW, kScreenH / 3);
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bgView];
    
    UIView *btnView = [[UIView alloc]init];
    btnView.frame = CGRectMake(0, 0, self.view.frame.size.width, 40 * SCREENH6);
    btnView.backgroundColor = [UIColor lightGrayColor];
    [self.bgView addSubview:btnView];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(10 * SCREENH6, 5 * SCREENH6, 60 * SCREENH6, 30 * SCREENH6);
    [sureBtn setTitle:@"取消" forState:UIControlStateNormal];
    [sureBtn setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(cancelBtn) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:sureBtn];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(self.view.frame.size.width - 70 * SCREENH6, 5 * SCREENH6, 60 * SCREENH6, 30 * SCREENH6);
    [cancelBtn setTitle:@"确定" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(sureBtn) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:cancelBtn];
    
    float height = kScreenH / 3 - 40 * SCREENH6;
    self.pickerView = [[UIPickerView alloc]init];
    self.pickerView.frame = CGRectMake(0, 40 * SCREENH6, kScreenW, height);
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.bgView addSubview:self.pickerView];
}

// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// 每列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return self.dataArray.count;
}


// 返回每一行的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    _weekIndex = row;

    NSString * titleName = self.dataArray[row];
    
    return titleName;
}


- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xE4E4E4);
    
    return lineView;
}

- (UILabel *)setupLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.textColor = ZRBlackColor;
    label.font = [UIFont systemFontOfSize:13 * SCREENH6];
    label.text = title;
    
    return label;
}

- (UIImageView *)setupImageView{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:@"多边形1"];
    
    return imageView;
}

- (UIButton *)setupDateButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button setTitleColor:ZRBlackColor forState:UIControlStateSelected];
    button.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    [button addTarget:self action:@selector(clickSetupDateViewButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


- (void)clickSetupDateViewButton:(UIButton *)button{
    
    if (button == self.dateStartButton) {
        
        self.type = @"dateStart";
        
        [UIView animateWithDuration:0.5 animations:^{
            self.bgView.frame = CGRectMake(0, kScreenH - kScreenH / 3, kScreenW, kScreenH / 3);
        }];
    }else if (button == self.dateEndButton){
        
        self.type = @"dateEnd";
        
        [UIView animateWithDuration:0.5 animations:^{
            self.bgView.frame = CGRectMake(0, kScreenH - kScreenH / 3, kScreenW, kScreenH / 3);
        }];
        
    }else if (button == self.timeStartButton){
        
        self.type = @"timeStart";
        
        [self presentDateController];
    }else if (button == self.timeEndButton){
        
        self.type = @"timeEnd";
        
        [self presentDateController];
    }
    
}

- (void)cancelBtn{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, kScreenH / 3);
    } completion:^(BOOL finished) {
    }];
}


- (void)sureBtn{
    
    NSString * dateStr = self.dataArray[_weekIndex];
    
    if ([self.type isEqualToString:@"dateStart"]) {
        
        self.dateStartButton.selected = YES;
        [self.dateStartButton setTitle:dateStr forState:UIControlStateNormal];
    }else{
        
        self.dateEndButton.selected = YES;
        [self.dateEndButton setTitle:dateStr forState:UIControlStateNormal];
    }
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, kScreenH / 3);
    } completion:^(BOOL finished) {
    }];
    
    [self changButtonState];
}



- (void)changButtonState{
    
    if (self.dateStartButton.selected && self.dateEndButton.selected && self.timeStartButton.selected && self.timeEndButton.selected) {
        
        self.saveButton.enabled = YES;
        [self.saveButton setBackgroundColor:ZRBlackColor];
    }
}


- (void)clickSaveButton{
    
    NSString * dateString = [NSString stringWithFormat:@"%@ %@ - %@ %@",self.dateStartButton.titleLabel.text,self.timeStartButton.titleLabel.text,self.dateEndButton.titleLabel.text,self.timeEndButton.titleLabel.text];
    
    NSDictionary * dict = @{@"merchants_open_time":dateString};
    
    [[ZRNetworkTool sharedTool] editMerchantsResultWithDict:dict WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"恭喜你修改成功！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                            [self.navigationController popViewControllerAnimated:YES];
                                                                      if (self.returnSubBlock != nil) {
                                                                          
                                                                          self.returnSubBlock(dateString);
                                                                      } 
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}

@end
